package com.yeqian.config;

import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.*;
import org.springframework.context.annotation.Configuration;;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;


import java.util.ArrayList;
import java.util.List;


@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(this.getPubParam());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("数据服务平台")
                .description("简单优雅的restful风格，提供在线调试")
                .termsOfServiceUrl("http://localhost:8110/")
                .version("1.0")
                .build();
    }


    private List<Parameter> getPubParam() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList();
        tokenPar.name("YEQ_TOKEN").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }
}
